export const formateDate = (datetime, dateType) => {
  function addDateZero(num) {
    return parseInt(num) < 10 ? "0" + num : num;
  }
  // 兼容性处理
  if (
    typeof datetime === "string" &&
    (datetime.indexOf("T") > -1 || datetime.includes("T"))
  ) {
    datetime = datetime.replace("T", " ").replace(/\-/g, "/"); //注意：指定一个具体的时间转换时间戳，需要yyyy/mm/dd hh:ii:ss格式，yyyy-mm-dd在IE和Safari下是有问题的。
  }
  if (datetime == null || datetime == "") {
    //时间错误的时候
    return " ";
  } else {
    let d = new Date(datetime);
    // let formatdatetime = new Date(datetime.substr(0,parseInt(datetime.indexOf('.'))).replace(/-/g,"/").replace('T'," "));//转化兼容IE时间格式
    let formatdatetime =
      d.getFullYear() +
      "-" +
      addDateZero(d.getMonth() + 1) +
      "-" +
      addDateZero(d.getDate()) +
      " " +
      addDateZero(d.getHours()) +
      ":" +
      addDateZero(d.getMinutes()) +
      ":" +
      addDateZero(d.getSeconds());
    if (dateType == "day") {
      formatdatetime =
        d.getFullYear() +
        "-" +
        addDateZero(d.getMonth() + 1) +
        "-" +
        addDateZero(d.getDate());
    }
    return formatdatetime;
  }
};

export const dayToDate = (dateTime, type) => {
  if (dateTime) {
  } else {
    dateTime = "";
  }
  let nowVal = new Date(dateTime) - new Date().getTime();
  return nowVal;
};
/**
 *
 * @param {*} time  时间
 */
export const durationTime = (nowTime = new Date(), time) => {
  let nowDate = new Date();
  let lastDate = new Date(time);
  let dateDual = nowDate - lastDate;
  //计算出相差天数
  var days = Math.floor(dateDual / (24 * 3600 * 1000));
  //计算出小时数
  var leave1 = dateDual % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));
  //计算相差秒数
  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);
  return days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒";
};
/**
 *
 * @name 获取当前月的第一天
 */
export const getFirstMonthDay = () => {
  let date = new Date();
  date.setDate(1); // 将当前时间的日期设置成第一天
  let year = date.getFullYear(); // 得到当前年份
  let month = date.getMonth() + 1; // 得到当前月份（0-11月份，+1是当前月份）
  month = month >= 10 ? month : "0" + month; // 补零
  let day =
    Number(date.getDate()) >= 10 ? date.getDate() : "0" + date.getDate();
  return year + "-" + month + "-" + day + " 00:00:00"; // 这里传入的是字符串
};
